<template>
  <div>
    <h2>器材管理</h2>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item to="/AdminMain/AdminHome">主页</el-breadcrumb-item>
      <el-breadcrumb-item>器材管理</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card style="margin-top: 20px;">
      <template #header>
        <div>
          <i class="el-icon-s-platform" style="margin-right: 5px;"></i>器材信息表
        </div>
      </template>

      <el-table :data="equipmentList" style="width: 100%;" v-loading="loading">
        <el-table-column prop="equipmentId" label="器材ID" />
        <el-table-column prop="equipmentName" label="名称" />
        <el-table-column prop="equipmentLocation" label="位置" />
        <el-table-column prop="equipmentStatus" label="状态">
          <template #default="scope">
            <span :style="{ color: scope.row.equipmentStatus !== '正常' ? 'red' : '#333' }">
              {{ scope.row.equipmentStatus }}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="equipmentMessage" label="备注信息" />
        <el-table-column label="操作" align="left" width="180">
          <template #default="scope">
            <div class="action-buttons">
              <el-button type="primary" size="small" @click="onEdit(scope.row)" style="margin-right:5px;">编辑</el-button>
              <el-button type="danger" size="small" @click="onDelete(scope.row)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
          style="margin-top: 20px; text-align: center"
          background
          :current-page="currentPage"
          :page-size="pageSize"
          :total="total"
          layout="total, prev, pager, next, jumper"
          @current-change="handlePageChange"
      >
        <template #total>
          共 {{ total }} 条
        </template>
        <template #jumper>
          跳转到
          <el-input
              class="el-pagination__editor is-in-pagination"
              v-model.number="jumperPage"
              type="number"
              size="small"
              @keyup.enter.native="jumpToPage"
              :min="1"
              :max="Math.ceil(total / pageSize)"
          />
          页
        </template>
      </el-pagination>

      <div style="text-align:center;margin-top:20px;">
        <el-button type="success" @click="onAdd">添加器材信息</el-button>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import axios from 'axios'

const router = useRouter()

const equipmentList = ref([])
const loading = ref(false)
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const jumperPage = ref(null)

const loadData = () => {
  loading.value = true
  axios.get('/server/equipment/search', {
    params: {
      page: currentPage.value,
      size: pageSize.value
    }
  })
      .then(response => {
        if (response.data.code === 200) {
          equipmentList.value = response.data.data.records
          total.value = response.data.data.total
        } else {
          ElMessage.error('获取器材信息失败：' + response.data.message)
        }
      })
      .catch(() => {
        ElMessage.error('服务器请求失败')
      })
      .finally(() => {
        loading.value = false
      })
}

const handlePageChange = (newPage) => {
  currentPage.value = newPage
  loadData()
}

const jumpToPage = () => {
  const maxPage = Math.ceil(total.value / pageSize.value)
  if (jumperPage.value >= 1 && jumperPage.value <= maxPage) {
    currentPage.value = jumperPage.value
    loadData()
  } else {
    ElMessage.warning('请输入有效页码')
  }
}

onMounted(() => {
  loadData()
})

const onEdit = (row) => {
  router.push({ path: '/AdminMain/EditEquipment', query: { equipmentId: row.equipmentId } })
}

const onDelete = (row) => {
  ElMessageBox.confirm(`确定要删除器材 ID 为 ${row.equipmentId} 的器材吗？`, '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    axios.get(`/server/equipment/delete?equipmentId=${row.equipmentId}`)
        .then(res => {
          if (res.data.code === 200) {
            ElMessage.success('删除成功')
            loadData()
          } else {
            ElMessage.error('删除失败')
          }
        })
        .catch(() => {
          ElMessage.error('请求失败')
        })
  }).catch(() => {
    ElMessage.info('已取消删除')
  })
}

const onAdd = () => {
  router.push('/AdminMain/AddEquipment')
}
</script>

<style scoped>
.action-buttons {
  padding-left: 0;
  margin-left: -12px;
}
</style>